<template>
  <el-dialog :title="dataForm.id ? '修改' : '新增'" :close-on-click-modal="false" :visible.sync="visible" width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="110px">
      <el-form-item label="账号" prop="username">
        <el-input v-model="dataForm.username" placeholder="账号"></el-input>
        <div>默认密码：123456</div>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="dataForm.role" style="width: 100%">
          <el-option v-for="item in roleOptions" :key="item.id" :label="item.role" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" :disable="dataForm.role==2" placeholder="年龄"></el-input>
      </el-form-item>
      <!-- <el-form-item label="毕业院校" prop="graduation_inst">
        <el-input v-model="dataForm.graduation_inst" :disable="dataForm.role==2" placeholder="毕业院校"></el-input>
      </el-form-item>
      <el-form-item label="学历" prop="education_qual">
        <el-input v-model="dataForm.education_qual" :disable="dataForm.role==2" placeholder="学历"></el-input>
      </el-form-item>
      <el-form-item label="职称" prop="professional_title">
        <el-input v-model="dataForm.professional_title" :disable="dataForm.role==2" placeholder="职称"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getDataInfo, savaDataInfo } from '@/api/user';
  import { getRoleOptions } from '@/api/role';
  export default {
    data() {
      return {
        visible: false,
        dataForm: {},
        dataRule: {
          username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
          name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
          role: [{ required: true, message: '角色不能为空', trigger: 'blur' }],
          password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
          phone: [{ required: true, message: '手机不能为空', trigger: 'blur' }],
          address: [{ required: true, message: '住址不能为空', trigger: 'blur' }],
        },
        roleOptions: [],
      };
    },
    mounted() {
      getRoleOptions()
        .then((res) => {
          if (res && res.code === 0) {
            this.roleOptions = res.data;
          }
        })
        .catch(() => { });
    },
    methods: {
      init(username) {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          this.dataForm = { username: username };
          if (this.dataForm.username) {
            getDataInfo(this.dataForm.username)
              .then((data) => {
                if (data && data.code === 0) {
                  this.dataForm = data.data;
                  this.$set(this.dataForm, 'role', data.data.roleList[0].roleId);
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.id ? delete this.dataForm.password : (this.dataForm.password = '123456');
            savaDataInfo(this.dataForm)
              .then((data) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false;
                      this.$emit('refreshDataList');
                    },
                  });
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
        });
      },
    },
  };
</script>